package com.bw.lib_common.base

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.launcher.ARouter
import com.gyf.immersionbar.ImmersionBar

/**
 * @author ykj
 * @date 2025-10-16 20:37
 * @description
 * activity基类，不带有mvi
 */
abstract class BaseActivity :AppCompatActivity(){
    private var statusBarHeight = 0 //状态栏的高度



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //设置布局
        if (getLayoutId() == -1){
            setContentView(getLayoutRootView())
        }else{
            setContentView(getLayoutId())
        }
        ARouter.getInstance().inject(this)
        //设置沉浸式状态栏
        initImmersiveBar()
        //获得状态栏的高度
        statusBarHeight = ImmersionBar.getStatusBarHeight(this)
        //调整高度
        adjustMargin()
        initEnv()
        initView()
        initData()
        initEvent()

    }

    /**
     * 调整高度
     */
    private fun adjustMargin() {
        if (!setNeedAdjustMargin() || statusBarHeight == 0) return
        //1：找view
        val rootView = findViewById<ViewGroup>(android.R.id.content)
        val child = rootView.getChildAt(0) //第一个子控件
        //2：调整
        child?.setPadding(
            child.paddingLeft,
            child.paddingTop + statusBarHeight,
            child.paddingRight,
            child.paddingBottom
        )

    }

    /**
     * 设置是否需要调整
     */
    protected open fun setNeedAdjustMargin():Boolean{
        return true
    }

    /**
     * 设置状态栏是否为深色模式
     */
    protected open fun setStatusBarDarkFont():Boolean{
        return true
    }
    /**
     * 沉浸式状态栏
     */
    protected open fun initImmersiveBar() {
        ImmersionBar.with(this)
            .transparentStatusBar()
            .statusBarDarkFont(setStatusBarDarkFont()) //深色模式
            .fitsSystemWindows(false) //沉浸式状态栏
            .keyboardEnable( true)//键盘
            .init()
    }

    /**
     * viewBinding、dataBinding设置布局view
     */
    abstract fun getLayoutRootView():View

    /**
     * 布局id设置布局view
     */
    protected open fun getLayoutId(layoutId:Int = -1):Int{
        return layoutId
    }


    /**
     * 初始化环境
     */
    protected open fun initEnv(){}

    /**
     * 初始化view
     */
    protected open fun initView(){}

    /**
     * 初始化数据
     */
    protected open fun initData(){}

    /**
     * 事件绑定、订阅
     */
    protected open fun initEvent(){}

    private val handler: Handler = Handler(Looper.getMainLooper())
    /**
     * 显示提示消息
     */
    fun showMsg(msg:String){
        if(Looper.getMainLooper().thread==Thread.currentThread()){ //判断当前线程是否为主线程
            Toast.makeText(this,msg, Toast.LENGTH_SHORT).show()
        }
        else{
            handler.post {
                Toast.makeText(this,msg, Toast.LENGTH_SHORT).show()
            }
        }
    }

    /**
     * 组件内跳转到指定页面
     */
    fun routeActivity(target:Class<*>){
        startActivity(Intent(this,target))
    }
    /**
     * 组件内跳转指定页面并传递参数
     */
    fun routeActivity(target: Class<*>,params:Bundle){
        val intent=Intent(this,target)
        intent.putExtra("params",params)
        startActivity(intent)
    }

    fun addFragment(fragment: Fragment, tag:String){
        //判断是否添加过
        if (!fragment.isAdded){
            supportFragmentManager.beginTransaction().add(fragment,tag).commitAllowingStateLoss()
        }
    }
    fun hideFragment(tag:String){
        supportFragmentManager.findFragmentByTag(tag)
            ?.let {
                supportFragmentManager.beginTransaction().hide(it).commitAllowingStateLoss()
            }
    }

    fun showFragment(tag:String){
        supportFragmentManager.findFragmentByTag(tag)?.let {
            supportFragmentManager.beginTransaction().show(it).commitAllowingStateLoss()
        }
    }
    fun replaceFragment(containerId:Int,fragment:Fragment){
        supportFragmentManager.beginTransaction().replace(containerId,fragment).commitAllowingStateLoss()
    }

    fun switchFragment(fl:Int,from: Fragment?, to: Fragment) {
        if (!to.isAdded) {
            supportFragmentManager.beginTransaction().hide(from!!).add(fl, to).commit()
        } else {
            supportFragmentManager.beginTransaction().hide(from!!).show(to).commit()
        }
    }

    fun closeThis(){
        finish()
    }


}